/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Controllers;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.RequestDispatcher;
import Models.*;
import Beans.*;
import java.util.Vector;
import java.util.Calendar;
/**
 *
 * @author VIC
 */
public class GhiPhieu_Controller extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        request.setCharacterEncoding("UTF-8");
        HttpSession session = request.getSession();
        try {
            if (session.getAttribute("langid") == null) {
                response.sendRedirect("index.jsp");
                return;
            }
            session.removeAttribute("DSMA");
            session.removeAttribute("DSNMA");
            session.removeAttribute("item");
            String act = request.getParameter("act");
            int langid = (Integer) session.getAttribute("langid");
             NhanVien nv = null;
            if (session.getAttribute("nv") != null) {
                 nv = (NhanVien) session.getAttribute("nv");
            }
            if(act.equals("checkBan")){
                String ban = request.getParameter("ban");
                boolean kq = Ban_Model.KiemTraBan(ban);
                if(kq)
                    out.print("1");
                else
                    out.print("0");
            }
            else if(act.equals("XemTTBan")){
                String maban = request.getParameter("ban");
                int ban = Integer.parseInt(maban);
                String state = Ban_Model.LayTinhTrang(ban, langid);
                out.print(state);
            }
            else if(act.equals("CapNhatTTB")){
                String dsts = "'updatesuc','updatefal','rightmsg'";
                ThamSoHT[] mdsts = ThamSoHT_Model.DSThamSo(dsts, langid);
                String rightmsg = mdsts[0].getTenTS();
                String orderfal = mdsts[1].getTenTS();
                String ordersuc = mdsts[2].getTenTS();
                if(! NhanVien_Model.CheckQuyen(68, nv.getMaNV()))
                {
                    out.print(rightmsg);
                    return;
                }
                String matt = request.getParameter("matt");
                String ban = request.getParameter("ban");
                int maban = Integer.parseInt(ban);
                boolean kq = Ban_Model.UpdateTTBanMoi(matt, ban);                 
                String state = Ban_Model.LayTinhTrang(maban, langid);
                if(kq)
                {
                    session.removeAttribute(ban);
                    NhatKyHT_Model.GhiNhatKy(3, nv.getMaNV(), "statustable");
                    out.print(ordersuc);
                }
                else {
                    out.print(state);
                }
            }

            else if(act.equals("goiMon")){
                 String mama = request.getParameter("mama");
                 String tenma = request.getParameter("tenma");
                 String tendvt = request.getParameter("tendvt");
                 String madvt = request.getParameter("madvt");
                 String gia = request.getParameter("gia");
                 String hinhanh = request.getParameter("hinhanh");
                 String ban = request.getParameter("ban");
                 int soluong = 1;
                 String[] item = new String[8];
                 item[0] = mama;
                 item[1] = tenma;
                 item[2] = madvt;
                 item[3] = tendvt;
                 item[4] = hinhanh;
                 item[5] = gia;
                 item[6] = "&&    "; // ghichu
                
                Vector cart = new Vector();
                if (session.getAttribute(ban) != null)
                {
                    cart = (Vector) session.getAttribute(ban);
                    for(int i =0; i <cart.size();i++)
                    {
                        String[] monan = (String[]) cart.get(i);

                        if(monan[0].equals(mama) && monan[2].equals(madvt))
                        {
                                soluong = Integer.parseInt(monan[7]);
                                soluong = soluong + 1;
                                item[7] = String.valueOf(soluong);
                                item[6] = monan[6];
                                cart.set(i, item);
                                out.print(item[7]);
                                return;
                        }
                    }
                }

                item[7] = String.valueOf(soluong);
                String kq = item[7];
                cart.add(item);
                session.setAttribute(ban,cart);
                out.print(kq);
            }
            else if(act.equals("botMon")){
                String mama = request.getParameter("mama");
                String madvt = request.getParameter("madvt");
                String ban = request.getParameter("ban");
                Vector cart = new Vector();
                
                int soluong = 0;
                if (session.getAttribute(ban) != null)
                {
                    cart = (Vector) session.getAttribute(ban);
                    int size = cart.size();
                    for(int i =0; i <cart.size();i++)
                    {
                        String[] monan = (String[]) cart.get(i);

                        if(monan[0].equals(mama) && monan[2].equals(madvt))
                        {
                                soluong = Integer.parseInt(monan[7]);
                                if(soluong ==0)
                                {
                                    out.print("-1");
                                    return;
                                }
                                    
                                if(soluong == 1)
                                {
                                    if(size == 1)
                                    {
                                        session.removeAttribute(ban);
                                        out.print("-1");
                                    }
                                    else {
                                        cart.removeElementAt(i);
                                    session.setAttribute(ban,cart);
                                    out.print("0");
                                    }
                                }
                                else
                                {
                                    soluong = soluong - 1;
                                    monan[7] = String.valueOf(soluong);
                                    cart.set(i, monan);
                                    session.setAttribute(ban,cart);
                                    out.print(monan[7]);
                                }
                        }
                    }
                }
                else {
                    out.print("0");
                }
            }
            else if(act.equals("ghichu")){
                String mama = request.getParameter("mama");
                String ghichu = request.getParameter("note");
                String ban = request.getParameter("ban");
                String madvt = request.getParameter("madvt");
                Vector cart = new Vector();
                if (session.getAttribute(ban) != null)
                {
                    cart = (Vector) session.getAttribute(ban);
                    for(int i =0; i <cart.size();i++)
                    {
                        String[] monan = (String[]) cart.get(i);

                        if(monan[0].equals(mama) && monan[2].equals(madvt))
                        {
                                    monan[6] = ghichu;
                                    cart.set(i, monan);
                                    session.setAttribute(ban,cart);
                                    out.print("1");
                        }
                    }
                }

            }
            else if(act.equals("xemPhieu"))
            {
                String ban = request.getParameter("ban");
                request.setAttribute("ban", ban);
                RequestDispatcher rd = request.getRequestDispatcher("waiter/cart.jsp");
                rd.forward(request, response);
            }
            else if(act.equals("lapPhieu")){
                String ban = request.getParameter("ban");
                int maban = Integer.parseInt(ban);
                int mattban = Ban_Model.LayMaTTBan(maban);
                if(mattban != 1 && mattban != 2 && mattban != 7 )
                {
                    String state = Ban_Model.LayTinhTrang(maban, langid);
                    out.print(state);
                    return;
                }
                String sokhach = "2";
                String manv = request.getParameter("nv");
                Vector v = (Vector) session.getAttribute(ban);
                String dsts = "'ordersuc','orderfal'";
                ThamSoHT[] mdsts = ThamSoHT_Model.DSThamSo(dsts, langid);
                String orderfal = mdsts[0].getTenTS();
                String ordersuc = mdsts[1].getTenTS();
                if(v.size() <= 0)
                {
                     out.print(orderfal);
                }
                else {
                    boolean kq = PhieuDatMA_Model.LapPhieu(ban, manv, sokhach, langid,v);
                    if(kq)
                    {
                        session.removeAttribute(ban);
                        if(nv != null)
                        NhatKyHT_Model.GhiNhatKy(7, nv.getMaNV(), "food");
                        out.print(ordersuc);
                    }
                    else {
                        out.print(orderfal);
                    }
                }
            }
            else if(act.equals("huyPhieu")){
                String ban = request.getParameter("ban");
                if (session.getAttribute(ban) != null)
                {
                    session.removeAttribute(ban);
                }
                
            }

        } finally {
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
